package cn.edu.xmu.oomall.liquidation.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.liquidation.mapper.RevenueItemPoMapper;
import cn.edu.xmu.oomall.liquidation.model.po.RevenueItemPo;
import cn.edu.xmu.oomall.liquidation.model.po.RevenueItemPoExample;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author HertzLiu
 * @create_time 2021/12/8 23:26
 * @package_name cn.edu.xmu.oomall.liquidation.dao
 * @project_name oomall
 * @product_name IntelliJ IDEA
 * @description
 */

@Repository
public class RevenueDao {

    @Autowired
    private RevenueItemPoMapper revenueItemPoMapper;

    public ReturnObject<PageInfo<RevenueItemPo>> getRevenueListPageInfo(RevenueItemPoExample revenueItemPoExample, Integer page, Integer pageSize) {
        try {
            PageHelper.startPage(page, pageSize);
            List<RevenueItemPo> revenueItemPoList = revenueItemPoMapper.selectByExample(revenueItemPoExample);
            PageInfo<RevenueItemPo> revenueItemPoListPageInfo = PageInfo.of(revenueItemPoList);
            return new ReturnObject<>(revenueItemPoListPageInfo);
        } catch(Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject<Object> getRevenueById(Long shopId, Long id) {
        try {
            RevenueItemPo revenueItemPo = revenueItemPoMapper.selectByPrimaryKey(id);
            if (revenueItemPo == null) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_NOTEXIST, "入账单id不存在");
            }
            if (!revenueItemPo.getShopId().equals(shopId) && shopId != 0) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_OUTSCOPE, "该入账单不属于本商铺");
            }
            return new ReturnObject<>(revenueItemPo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject insertRevenueItemPo(RevenueItemPo revenueItemPo, Long adminId, String adminName) {
        try {
            Common.setPoCreatedFields(revenueItemPo, adminId, adminName);
            int ret = revenueItemPoMapper.insertSelective(revenueItemPo);
            if (ret == 0) {
                return new ReturnObject<>(ReturnNo.FIELD_NOTVALID,"insertRevenueItemPo报错");
            }
            return new ReturnObject<>(ReturnNo.OK);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR,"insertRevenueItemPo报错");
        }
    }

    public ReturnObject<List<RevenueItemPo>> getRevenueListByExample(RevenueItemPoExample revenueItemPoExample) {
        try {
            List<RevenueItemPo> revenueItemPoList = revenueItemPoMapper.selectByExample(revenueItemPoExample);
            if (revenueItemPoList.size() == 0) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_NOTEXIST, "无法找到出账对应的入账的订单");
            }
            return new ReturnObject<>(revenueItemPoList);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }
}
